package com.oo.parkinglot;
//封装
//可读性
//对象比较

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public abstract class AbstractParkingRule {

    List<ParkingLot> parkingLots = new ArrayList<>();

    /**
     * 子类去实现取车规则
     * @return
     */
    protected abstract ParkingLot whichParkingLot2Park();

    public boolean park(Car car) {
        return park(null, car);
    }

    public boolean park(ParkingLot targetParkingLot, Car car) {
        ParkingLot parkingLot = whichParkingLot2Park();
        if (null == parkingLot || (targetParkingLot != null && !parkingLot.equals(targetParkingLot))) {
            return false;
        }
        return parkingLot.park(car);
    }

    public Car pickUp(String carNum) {
        return pickUp(null, carNum);
    }

    public Car pickUp(ParkingLot targetParkingLot, String carNum) {
        ParkingLot parkingLot = whichParkingLot2PickUp(carNum);
        if (null == parkingLot || (targetParkingLot != null && !parkingLot.equals(targetParkingLot))) {
            return null;
        }
        return parkingLot.pickUp(carNum);
    }

    protected ParkingLot whichParkingLot2PickUp(String carNum) {
        List<ParkingLot> parkingLotList = parkingLots
                .stream()
                .filter((parkingLot -> parkingLot.getCars().keySet().contains(carNum)))
                .collect(Collectors.toList());
        return 1 == parkingLotList.size() ? parkingLotList.get(0) : null;
    }
}
